<?php

namespace Frieza\Modules\BasicCheck\Controllers;


use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Doraemon\service\BasicCheck\CheckBackgroundInfo;
use Doraemon\service\BasicCheck\CheckCommonInfo;
use Doraemon\service\Member as sMember;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Displayers\DropdownActions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Frieza\Modules\BasicCheck\Actions\BackgroundApproved;
use Frieza\Modules\BasicCheck\Actions\BackgroundBatchApproved;
use Frieza\Modules\BasicCheck\Actions\SetDefaultBackground;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use App\Admin\Extensions\Tools\ReleasePost;
use App\Admin\Models\Administrator;
use Encore\Admin\Grid\Tools;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Table;

class CheckBackgroundController extends Controller
{
    public function backgroundList(Request $request, Content $content)
    {
        $page = $request->input("page", 1);
        if ($page < 1) {
            $page = 1;
        }

        $pageSize = $request->input("per_page", 20);
        if ($pageSize < 0) {
            $pageSize = 20;
        }
        $search = $request->all();

        $list = CheckBackgroundInfo::getList($search,$page, $pageSize);

        $gridModel = new GridDataModel($list['list'], $list['total'], ['perPage' => $pageSize]);

        $listGrid = Admin::grid($gridModel, function (Grid $grid) use ($gridModel) {
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableColumnSelector();
            $grid->expandFilter();
            $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
                $batch->disableDelete();

                $batch->add(new BackgroundBatchApproved());
            });
            $grid->filter(function (Grid\Filter $filter) {

                $filter->disableIdFilter();

                $filter->column(8, function ($filter) {

                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('uid', '用户ID');
                    });
                    $filter->column(1 / 2, function (\Encore\Admin\Grid\Filter $filter) {

                        $filter->dropDown('add_time', [
                            'add_time' => '更改时间'
                        ])->betweenDatetime();
                    });
                });

                $filter->column(8, function ($filter) {

                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('check_status', '审核状态')->radio([0 => "未审核", 1 => "审核通过", 2 => "审核不通过"]);
                    });


                });


                $filter->expend();
            });
            $grid->column('id', 'ID');
            $grid->column('background', '背景图')->display(function () {
                return '<img src = "' . $this->img_url . '"width = 100px heigh = 100px/>';
            });
            $grid->column('uid', 'uid')->expand(function () {
                $info = CheckBackgroundInfo::getInfoByUid(['uid' => $this->uid, "id[!]" => $this->id,"conclusion_type[!]" => 2]);
                 return new Table(["ID", "违规原因", "更改时间", "审核时间", "审核状态"],$info);
            });
            $grid->column('nick', '昵称')->display(function () {
                $userInfo = sMember::getUserInfoByUid($this->uid);

                return "<a href='/admin/user/Detail/$this->uid' target='_blank' >" . $userInfo['nick'] . "</a>";
            })->width(100);

            $grid->column('reasons_violation', '违规原因')->display(function (){
                if ($this->reasons_violation == 7) {
                    $reason = $this->other_reason;
                } else {
                    $reason = CheckCommonInfo::VIOLATION_REASON[$this->reasons_violation];
                }

                return $reason;
            });
            $grid->column('add_time', '更改时间')->display(function () {
                if ($this->add_time) {
                    return date("Y-m-d H:i:s", $this->add_time);
                } else {
                    return '--';
                }
            });
            $grid->column('check_time', '审核时间')->display(function () {
                if ($this->check_time) {
                    return date("Y-m-d H:i:s", $this->check_time);
                } else {
                    return '--';
                }
            });
            $grid->column('check_status', '审核状态')->display(function () {
                if (CheckCommonInfo::CHECK_STATUS_YES ==  $this->check_status) {
                    return "审核通过";
                } elseif (CheckCommonInfo::CHECK_STATUS_NO == $this->check_status) {
                    return "审核不通过";
                } elseif (CheckCommonInfo::CHECK_STATUS_INVALID == $this->check_status) {
                    return "无效";
                } elseif (CheckCommonInfo::CHECK_STATUS_UNAUDITED == $this->check_status) {
                    return "<span style='color: red'> 未审核 </span>";
                }
            });
            $grid->setActionClass(DropdownActions::class);
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();

                $actions->append('<a href=""><i class="fa fa-eye"></i></a>');
                if($actions->row->check_status == CheckCommonInfo::CHECK_STATUS_NO){
                    $actions->add(new BackgroundApproved());
                } elseif ($actions->row->check_status == CheckCommonInfo::CHECK_STATUS_YES) {
                    $actions->add(new SetDefaultBackground());
                } else {
                    $actions->add(new BackgroundApproved());
                    $actions->add(new SetDefaultBackground());
                }


            });
        });
        return $content
            ->header('背景图审核')
            ->breadcrumb(['text' => '背景图审核'])
            ->body($listGrid);
    }
}